package com.ruoyi.project.system.info.service.impl;

import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.system.dict.utils.DictUtils;
import com.ruoyi.project.system.function.bean.CategoryMap;
import com.ruoyi.project.system.function.domain.Detail;
import com.ruoyi.project.system.function.domain.DetailDB;
import com.ruoyi.project.system.function.domain.OverviewDB;
import com.ruoyi.project.system.function.domain.UIDetail;
import com.ruoyi.project.system.function.mapper.FunctionMapper;
import com.ruoyi.project.system.info.Bean.Bean;
import com.ruoyi.project.system.info.domain.BaseFamilyInfoManagement;
import com.ruoyi.project.system.info.domain.FormData;
import com.ruoyi.project.system.info.domain.UIData;
import com.ruoyi.project.system.info.mapper.BaseFamilyInfoManagementMapper;
import com.ruoyi.project.system.info.service.IBaseFamilyInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-17
 */
@Service
public class BaseFamilyInfoManagementServiceImpl implements IBaseFamilyInfoService {
    @Resource
    private BaseFamilyInfoManagementMapper baseFamilyInfoManagementMapper;
    @Resource
    private FunctionMapper functionMapper;

    @Override
    public Map<String, Object> echo(String fid, int year) {
        Map<String, Object> map = new HashMap<>();
        OverviewDB db = selectOverviewDB(fid, year);
        List<DetailDB> detailDBS = selectDetailDBList(db.getId());
        List<UIDetail> list = new ArrayList<>();
        for (DetailDB detailDB : detailDBS)
            list.add(new UIDetail(detailDB));

        map.put("baseInfo", db);
        map.put("detailInfo", list);
        return map;
    }

    @Override
    public OverviewDB selectOverviewDB(String fid, int year) {
        return functionMapper.selectOverviewDB(fid, year);
    }

    @Override
    public List<DetailDB> selectDetailDBList(int id) {
        return functionMapper.selectDetailDBList(id);
    }

    /**
     * @description:
     * 对数据库表`busi_income_data`操作：更新此户编号的相关信息
     * 对数据库表`busi_income_detail`操作：（此数据库表通过incomeDataId与上一个表id实现多对一）更新了几条三级收入就对应了几条字段
     * @author chen0
     * @date 2023/6/7 12:44
     * @version 1.0
     */
    @Override
    public void dataEntry(List<UIData> list, OverviewDB overviewDB, String expense) {
        List<DetailDB> dbList = new ArrayList<>();
        Double op = 0.0;
        Double prop = 0.0;
        Double salary = 0.0;
        Double tran = 0.0;
        for (UIData data : list) {
            String[] ids = data.getId().split("_");
            int one = Integer.parseInt(ids[0]);
            int two = Integer.parseInt(ids[1]);
            int three = 0;
            if (ids.length > 2)
                three = Integer.parseInt(ids[2]);
            Double quantity = null;
            if (data.getQuantity() != null && !data.getQuantity().equals(""))
                quantity = Double.parseDouble(data.getQuantity());
            double amount = 0.0;
            if (data.getAmount() != null && !data.getAmount().equals(""))
                amount = Double.parseDouble(data.getAmount());

            switch (one) {
                case 1:
                    op += amount;
                    break;
                case 2:
                    prop += amount;
                    break;
                case 3:
                    salary += amount;
                    break;
                case 4:
                    tran += amount;
                    break;
                default:
                    break;
            }

            Double tmpAmount = amount == 0.0 ? null : amount;
            /**
             * @description:
             * 收入表编号 -- 区别于户编号
             * 一级收入
             * 二级收入
             * 三级收入
             * @author chen0
             * @date 2023/6/7 12:29
             * @version 1.0
             * @return id --> 数据唯一标识
             */
            Integer id = baseFamilyInfoManagementMapper.selectDetailDBID(overviewDB.getId(), one, two, three);
            // `busi_income_detail`的对象字段
            DetailDB db = new DetailDB(id, overviewDB.getId(), one, two, three, quantity, tmpAmount);
            // 每一条字段都存入list列表中
            dbList.add(db);
            // 错误数据无需添加
            if (quantity == null && amount == 0.0 && id == null)
                dbList.remove(db);
        }
        double output = 0.0;
        if (expense != null)
            output = Double.parseDouble(expense);
        overviewDB.setExpense(output);
        overviewDB.setOpIncome(op);
        overviewDB.setPropertyIncome(prop);
        overviewDB.setSalaryIncome(salary);
        overviewDB.setTransferIncome(tran);
        overviewDB.setSumIncome(op + prop + salary + tran);
        overviewDB.setAvgIncome((op + prop + salary + tran - output) / baseFamilyInfoManagementMapper.selectNum(overviewDB.getFid()));
        baseFamilyInfoManagementMapper.dataEntry(dbList); // 将list列表中所有的字段全都存入数据库`busi_income_detail`表中
        functionMapper.updateOverviewDB(overviewDB); // 更新数据库`busi_income_data`表中此农户对应户编号的字段信息
    }

    @Override
    public BaseFamilyInfoManagement selectBaseFamilyInfo(Integer id) {
        return baseFamilyInfoManagementMapper.selectBaseFamilyInfo(id);
    }

    /**
     * 查询【请填写功能名称】
     *
     * @param fid 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public BaseFamilyInfoManagement selectBaseFamilyInfoById(String fid) {
        return baseFamilyInfoManagementMapper.selectBaseFamilyInfoById(fid);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param baseFamilyInfo 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<BaseFamilyInfoManagement> selectBaseFamilyInfoList(BaseFamilyInfoManagement baseFamilyInfo) {
        return baseFamilyInfoManagementMapper.selectBaseFamilyInfoList(baseFamilyInfo);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param baseFamilyInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertBaseFamilyInfo(BaseFamilyInfoManagement baseFamilyInfo) {
        return baseFamilyInfoManagementMapper.insertBaseFamilyInfo(baseFamilyInfo);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param baseFamilyInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateBaseFamilyInfo(BaseFamilyInfoManagement baseFamilyInfo) {
        return baseFamilyInfoManagementMapper.updateBaseFamilyInfo(baseFamilyInfo);
    }

    /**
     * 删除【请填写功能名称】对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBaseFamilyInfoByIds(String ids) {
        return baseFamilyInfoManagementMapper.deleteBaseFamilyInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param fid 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteBaseFamilyInfoById(String fid) {
        return baseFamilyInfoManagementMapper.deleteBaseFamilyInfoById(fid);
    }
}
